package game.ui
{
	
	import control.IRunCtrl;
	
	import models.ProfileModel;
	
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Sprite;
	import starling.textures.Texture;
	
	public class NumberMC extends Sprite implements IRunCtrl
	{
		
		private var sign:String;
		private var numimage:Image;
		private var Ximage:Image;
		//
		public function NumberMC(){
			
		}
		
		
		/**
		 * 
		 */		
//		private var _scoreNum:Sprite;
//		public function get scoreNum():Sprite
//		{
//			return _scoreNum;
//		}
		public var Score:Number=0;
		private var oldScore:Number=0;
		private var wnum:int=0;
		private var digits:Vector.<MovieClip>=new Vector.<MovieClip>();
		private var oldLength:int;
//		private var step:int;
		
		public function setScoreNum(num:Number=0):void{
			Score=oldScore+num;
			
			if(digits.length==0){
				var a:Vector.<Texture>=GameUI.instant.getnumberframes();
				var nummc:MovieClip=new MovieClip(a);
				this.addChild(nummc);
				digits.push(nummc);
			}
			
//			var csl:int=currScore.toString().length;
//			var gap:int=csl-wnum;
//			for (var i:int = 1; i <= gap; i++) 
//			{
//				var a:Vector.<Texture>=GameUI.instant.getnumberframes();
//				var nummc:MovieClip=new MovieClip(a);
//				this.addChild(nummc);
//				nummc.x=-nummc.width*(i+oldLength);
//			}
//			oldLength=digits.length;
//			wnum=csl;
			sign="score";
//			step=0;
		}
		
		/**
		 * 
		 * 
		 */		
		public function setMultiple(currNum:int):void{
			var a:Vector.<Texture>=GameUI.instant.getnumberframes();
			var nt:Texture=a[currNum];
			numimage=new Image(nt);
			Ximage=new Image(a[10])
			this.addChild(numimage);
			this.addChild(Ximage);
			numimage.x=Ximage.width+5;
			sign="single";
		}
		
		/**
		 * 
		 * @param CurrNum
		 * 
		 */		
		private var num:int=1;
		private var currnum:int=0; 
		public function Run(CurrNum:int):void
		{
			currnum++;
			if(currnum<num){
				return;
			}
			currnum=0;
			switch(sign)
			{
				case "single":
				{
//					currnum++;
//					if(currnum>=num){
						currnum=0;
						this.y-=10;
						this.alpha-=0.1;
//					}
					if(this.alpha<=0){
						ProfileModel.instant._runVec.splice(CurrNum,1);
					}
					break;
				}
				case "score":
				{
					if(oldScore==Score){
						ProfileModel.instant._runVec.splice(CurrNum,1);
						return;
					};
					
					oldScore++;
					
					var slen:int=digits.length;
					var scorelen:int=String(oldScore).length;
//					if(scorelen>slen){
						var c:int=scorelen-slen;
						for (var j:int = 0; j <c; j++) 
						{
							var a:Vector.<Texture>=GameUI.instant.getnumberframes();
							var nummc:MovieClip=new MovieClip(a);
							this.addChild(nummc);
							digits.unshift(nummc);
							nummc.x=-nummc.width*(j+slen);
						}
						
//					}
					slen=digits.length;


					for (var i:int = 0; i < slen; i++) 
					{
						var s:String=String(oldScore);
						digits[i].currentFrame=int(s.charAt(i));
					}
					
					
					break;
				}
					
				default:
				{
					break;
				}
			}
		}
	}
}